package br.teste.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import br.pucpr.log.Logar;

public class Sera {
	public static void main(String[] args) {
		int port = 6666;
		InetAddress address = null;
		Socket s = null;
		InputStream in = null;
		OutputStream out = null;
		try {
			address = InetAddress.getByName("192.168.1.101");
			s = new Socket(address, port);
			out = s.getOutputStream();
			in = s.getInputStream();

			byte[] enviar = "7;carlos.pedroso;redes;24062010;".getBytes();
			out.write(enviar);

			String retorno = "";
			int tamanho;
			
			while(true){
				byte[] lera = new byte[1000];
				tamanho = in.read(lera);
				if ( tamanho < 0 ) break;
				retorno += new String(lera, 0, tamanho, "UTF-8");
			}

			Logar.info(Sera.class, retorno);

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (s != null)
					s.close();
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
